import {
  getColumnWidth,
  getColumnDefByIndex,
  getColumnWidthByDefine,
  excelAddressToRowCol,
  getMergeInside,
  xmlToObject,
  getRowHeight,
  getRowDefByIndex
} from "./utils.js";
import StyleConvertor from "./StyleConvertor.js";
import { watch } from "./sysUtils.js"
import Vue from "vue";
export default class JTableHelper {
  constructor(table, workbook,config) {
    this.table = table;
    this.config = config;
    this.scale = config && config.scale || 1;
    this.edittingCell = null;
    this.styleMap = new Map();
    this.borderStyleMap = new Map();
    this.selection = [-1,-1,-1,-1];
    this.allCells = {};
    //计算行数、列数
    if(this.table && this.table.dimensions && this.table.dimensions.model){
      if(this.table.dimensions.model.right){
        this.colCount = this.table.dimensions.model.right;
      }
      if(this.table.dimensions.model.bottom){
        this.rowCount = this.table.dimensions.model.bottom;
      }
    }else{
      this.colCount= 0;
      this.rowCount= 0;
    }

    this.updateRowsAndCols();
    //读取主题
    if(workbook && workbook.themes && Object.keys(workbook.themes).length>0)
    {
      this.theme = xmlToObject(workbook.themes[Object.keys(workbook.themes)[0]]);
    }
  }
  updateRowsAndCols(){
    // this.cols = Vue.observable(this._getColumns());
    // this.rows = Vue.observable(this._getRows());
    this.cols = this._getColumns();
    this.rows = this._getRows();
  }
  _getColumns() {
    if (this.table && this.table.rows) {
      let colCount= this.colCount;
      return [...Array(colCount).keys()].map((index) => {
        const colDef = getColumnDefByIndex(index,this.table.cols);
        return { width: getColumnWidthByDefine(colDef),colDef:colDef};
      });
    } else {
      return [];
    }
  }
  _getRows(){
    if (this.table && this.table.rows) {
      return [...Array(this.rowCount).keys()].map((index) => {
        const rowDef = getRowDefByIndex(index,this.table.rows);
        const cells = {};
        if(rowDef && rowDef.cells && rowDef.cells.length>0)
        {
          rowDef.cells.forEach(cell=>{            
            cells[excelAddressToRowCol(cell.address)[1]+'']=cell;
          })
        }
        return { height: getRowHeight(rowDef?rowDef.height:null), rowDef:rowDef ,cells};
      });
    } else {
      return [];
    }
  }
  addNewCell(address){
    const [row,col] = excelAddressToRowCol(address)
    const rowDef = this.table.rows.filter(t=>t.number== row+1)[0];
    console.log('rowDef',rowDef)
    const newCell = {address:address,value:""};
    rowDef.cells.push(newCell)
    //注意新添加的属性使用set才能具有响应式
    Vue.set(this.rows[row].cells,col+'',newCell);
    return newCell;
  } 

  getColumns(){
    return this.cols;
  }
  getRows(){
    return this.rows;
  }
  getStyle(style,align=true,font=true,fill=true) {
    if (!style) return {};
    if (this.styleMap.has({style,align,font,fill})) {
      return this.styleMap.get({style,align,font,fill});
    }
    const cellstyle = StyleConvertor.getStyle(style,this.theme,align,font,fill);
    this.styleMap.set({style,align,font,fill}, cellstyle);
    return cellstyle;
  }
  getRichTextStyle(part){
    if(!part) return {};
    let style;
    if(part.font){
      style = StyleConvertor.getFont(part.font,this.theme);
    }
    return style;
  }
  getSideBorder(row,col){//第一行、第一列的边框去掉
    let boder = {};
    if(row===0){
      boder.borderTop = 'none';
    }
    if(col===0){
      boder.borderLeft = 'none';
    }
    return boder;
  }
  getBorderStyleOld(cell) {
    let resultstyle = {};
    const style = cell ? cell.style : null;
    const [row, col] = excelAddressToRowCol(cell.address);
    if (!style) return {};
    if (this.borderStyleMap.has(style)) {
        return {...this.borderStyleMap.get(style),...this.getSideBorder(row,col)};
    }
    const merge = getMergeInside(row, col, this.table.merges);
    if (merge != null) {
      if (row === merge.startRow && col === merge.startCol) {
        if (merge.endCol > merge.startCol) {
          //水平合并
          let cell = this.table.rows[row].cells[merge.endCol];
          if (
            cell &&
            cell.style &&
            cell.style.border
          ) {
            resultstyle ={...resultstyle,...StyleConvertor.getBorderStyle(cell.style)};
          }
        }
        if (merge.endRow > merge.startRow) {
          //垂直合并
          let cell = this.table.rows[merge.endRow].cells[col];
          if (
            cell &&
            cell.style &&
            cell.style.border
          ) {
            resultstyle ={...resultstyle,...StyleConvertor.getBorderStyle(cell.style)};
          }
        }
      }
    }
    resultstyle = { ...resultstyle, ...StyleConvertor.getBorderStyle(style) };
    this.borderStyleMap.set(style, resultstyle);
    return{...resultstyle,...this.getSideBorder(row,col)};
  }
  getBorderStyle(cell) {
    const style = cell ? cell.style : null;
    const [row, col] = excelAddressToRowCol(cell.address);
    if (!style) return {};
    let borderStyle;
    if (this.borderStyleMap.has(style)) {
      borderStyle = this.borderStyleMap.get(style)
    }else{
      borderStyle = StyleConvertor.getBorderStyle(style);
      this.borderStyleMap.set(style,borderStyle)
    }
    let resultstyle = {...borderStyle};

    const merge = getMergeInside(row, col, this.table.merges);
    if (merge != null) {
      if(row !== merge.startRow)
      {
        resultstyle['border-top'] = undefined;
      }
      if(row !== merge.endRow)
      {
        resultstyle['border-bottom'] = undefined;
      }
      if(col !== merge.endCol)
      {
        resultstyle['border-right'] = undefined;
      }
      if(col !== merge.startCol){
        resultstyle['border-left'] = undefined;
      }
    }
    return{...resultstyle,...this.getSideBorder(row,col)};
  }
  getEdittingCell(){
    return this.edittingCell;
  }
  setEdittingCell(cell){
    this.edittingCell = cell;
  }
}
